/**
 * Created by Thyiad on 2018/9/10 0010.
 */

import Cookies from 'js-cookie';
import { Message, MessageBox, Notification } from 'element-ui';

let utils = {
  cache(key, val) {
    if (key === 'clear') {
      window.localStorage.clear();
    } else if (val === null) {
      window.localStorage.removeItem(key);
    } else if (val) {
      window.localStorage[key] = val;
    } else {
      return window.localStorage[key];
    }
  },
  cacheSession(key, val) {
    if (key === 'clear') {
      window.sessionStorage.clear();
    } else if (val === null) {
      window.sessionStorage.removeItem(key);
    } else if (val) {
      window.sessionStorage[key] = val;
    } else {
      return window.sessionStorage[key];
    }
  },
  cookie(key, val){
    if(val===null){
      Cookies.remove(key);
    }else if(val){
      Cookies.set(key,val);
    }else{
      return Cookies.get(key);
    }
  },
  guid(){
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
      var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
      return v.toString(16);
    });
  },
  toast(msg, type="info"){
    Message({
      message: msg,
      type: type,
    })
  },
  notify(msg, title="", type="info"){
    Notification({
      title: title,
      message: msg,
      type: type
    })
  },
  alert(msg, cb, title="", type="info", okText="确定"){
    MessageBox({
      title: title,
      message: msg,
      type: type,
      callback: cb,
      confirmButtonText: okText,
    })
  },
  confirm (msg, ok, cancel, title="", type="warning", okText="确定", cancelText="取消") {
    MessageBox({
      title: title,
      message: msg,
      type: type,
    }).then(ok).catch(cancel);
  },
}

export default utils;
